/*
 * Author:  Brett Rettura, Natalie
 * Company:  1186 Entertainment
 * Date Modified:  3/14/07
 * Filename:  Login.java
 */

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Login extends JPanel implements ActionListener
{
	public ClientApplet CA;

	//declare some components
	JLabel lblHeading;
	JLabel lblUserName;
	JLabel lblUserPwd;
	JLabel lblImg;

	String DELIM = "&";

	String usrName;
	char[] usrPwd;

	JTextField txtUserName;
	JPasswordField txtUsrPwd;

	Font f;
	Color r;
	JButton btnLogin;
	JButton btnGuest;
	JButton btnRegister;

	public Login(ClientApplet CA1)
	{
		CA = CA1;

		JPanel logPanel = new JPanel(new BorderLayout());

		this.setLayout(new BorderLayout());
		JPanel panel = new JPanel(new GridLayout(4, 1));

		lblHeading=new JLabel("Login or Register");
		Font f = new Font("Dialog" , Font.BOLD , 14);
		lblHeading.setFont(f);
		Color c=new Color(10,102,255);
		lblHeading.setForeground(c);
		lblHeading.setVerticalAlignment(SwingConstants.TOP);

		//
		logPanel.add(lblHeading, BorderLayout.NORTH);

		//CONSTRUCT FIELDS
		lblUserName = new JLabel("Enter Username");
		panel.add(lblUserName);
		txtUserName=new JTextField(15);
		panel.add(txtUserName);

		lblUserPwd=new JLabel("Enter Password ");
		panel.add(lblUserPwd);
		txtUsrPwd = new JPasswordField(15);
		panel.add(txtUsrPwd);

		//ADD FIELDS
		logPanel.add(panel, BorderLayout.CENTER);

		//CONSTRUCT BUTTONS
		JPanel btnPanel=new JPanel();

		btnLogin=new JButton("Login");
		btnPanel.add(btnLogin);
		btnLogin.addActionListener(this); //add listener to the Login button

		btnGuest=new JButton("Guest");
		btnPanel.add(btnGuest);
		btnGuest.addActionListener(this); //add listener to the Guest button

		btnRegister=new JButton("Register");
		btnPanel.add(btnRegister);
		btnRegister.addActionListener(this);

		//ADD BUTTONS
		logPanel.add(btnPanel, BorderLayout.SOUTH);

		//CONSTRUCT IMAGE
		JPanel panelL = new JPanel();

		ImageIcon icon = new ImageIcon(CA.getImage(CA.getDocumentBase(), "files/login_people.jpg"));
		//ImageIcon icon = new ImageIcon("files/login_people.jpg");
		JLabel label = new JLabel();
		label.setIcon(icon);
		panelL.add(label);

		//
		this.add(panelL, BorderLayout.EAST);
		this.add(logPanel, BorderLayout.WEST);

		//set background white
		this.setBackground(Color.WHITE);
		logPanel.setBackground(Color.WHITE);
		panelL.setBackground(Color.WHITE);
		btnPanel.setBackground(Color.WHITE);
		panel.setBackground(Color.WHITE);

		txtUserName.transferFocus();
		setSize(500,500);
		setVisible(true);

	}//end or Login()

	public void actionPerformed(ActionEvent e1)
	{

		JButton button=(JButton)e1.getSource(); //get the source of the event
		if(button.equals(btnGuest))
		{
			//CA.processMessage("13"+DELIM);
			CA.send("13");
			CA.loadPanel(new GameSelect(CA));
		}
		else if(button.equals(btnRegister))
		{
			//CA.processMessage("9"+DELIM);
			CA.loadPanel(new Register(CA));
		}
		else if(e1.getSource().equals(btnLogin))
		{
			if(valid())
			{
				try
				{
					usrName = txtUserName.getText();
					usrPwd = txtUsrPwd.getPassword();
					CA.send("1"+ DELIM + usrName + DELIM + new String(usrPwd));
				}
				catch(Exception e)
				{
					System.out.println("Exception "+e);
				}
			}//end of if
		}
		else if(e1.getSource().equals(txtUserName))
		{
			txtUsrPwd.transferFocus();
		}
		else if(e1.getSource().equals(txtUsrPwd))
		{
			btnLogin.transferFocus();
		}

	}//end of actionPerformed()

	boolean valid() //test the validity of the user information
	{
		try
		{
			usrName=txtUserName.getText();
			usrPwd=txtUsrPwd.getPassword();

			String strUsrPwd=new String(usrPwd);

			if(usrName.length() == 0 || strUsrPwd.length() == 0)
			{
				JOptionPane.showMessageDialog(this,"Please provide a username and password.", "Message", JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}//end of try
		catch(Exception e)
		{
			System.out.println("error in valid"+e);
		}//end of catch
		return true;
	}//end of valid()


	//error msg- User Exists
	void showUsrExists()
	{
		JOptionPane.showMessageDialog(this,"User exists.", "Message", JOptionPane.ERROR_MESSAGE);
	}

	int flg=0;

	//error msg- Incorrect Entry
	void showErrordlg()
	{
		JOptionPane.showMessageDialog(this,"Incorrect entry.", "Message", JOptionPane.ERROR_MESSAGE);
	}

	//error msg- Incorrect Age entered
	void showErrordlgInt()
	{
		JOptionPane.showMessageDialog(this,"Age incorrect.", "Message", JOptionPane.ERROR_MESSAGE);
	}

	//public static void main(String args[])
	//{
	//    new Login();
	//}
}//end of class
